package com.pomelo.tiku.activities

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.pomelo.tiku.R
import com.pomelo.tiku.adapters.BaseAdapter
import com.pomelo.tiku.adapters.ConcreteOccupationAdapter
import com.pomelo.tiku.adapters.IndustryAdapter
import com.pomelo.tiku.utils.ClickUtils
import com.pomelo.tiku.views.CommonToast
import kotlinx.android.synthetic.main.activity_oritation_vocation.*
import kotlinx.android.synthetic.main.activity_vacation_select.*

class OritationVocationActivity : BaseActivity(),View.OnClickListener {
    private val TAG = this::class.java.name
    private val REQUEST_CODE_ONE = 1
    private val REQUEST_CODE_TWO= 2
    private val REQUEST_CODE_THREE = 3
    private val RESULT_CODE = 1000
    private var vacation_Selected: String = ""

    private var listIndustry: ArrayList<String>? = null
    private var industryAdapter: IndustryAdapter? = null

    private var listConcreateOccupation: ArrayList<String>? = null
    private var concreateOccupationAdapter: ConcreteOccupationAdapter? = null

    private var isVacationSelected = false
    private var isIndustrySelected = false
    private var isConcreateOccupationSelected = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_oritation_vocation)
        initView()
    }

    private fun initView() {
        txt_orientate_type_cocation_and_function.setOnClickListener(this@OritationVocationActivity)
        txt_industry.setOnClickListener(this@OritationVocationActivity)
        txt_select_particular_vacation.setOnClickListener(this@OritationVocationActivity)
        btn_sure.setOnClickListener(this@OritationVocationActivity)
    }

    override fun onClick(view: View?) {
       when(view!!.id) {
           R.id.txt_orientate_type_cocation_and_function -> {
               if(ClickUtils.isDoubleClick(txt_orientate_type_cocation_and_function)) {
                   return
               }
               startActivityForResult(Intent(this@OritationVocationActivity,VacationSelectActivity::class.java),REQUEST_CODE_ONE)
               ll_concrete_occupation.visibility =View.GONE
               ll_industry.visibility = View.GONE
               ll_two.visibility = View.VISIBLE
               ll_three.visibility = View.VISIBLE
           }
           R.id.txt_industry -> {
               if(ClickUtils.isDoubleClick(txt_industry)) {
                   return
               }
               if(!isVacationSelected) {
                   CommonToast.show(this@OritationVocationActivity,"请先选择职业类型")
                   return
               }
               ll_industry.visibility = View.VISIBLE
               ll_two.visibility = View.GONE
               ll_three.visibility = View.GONE
               ll_concrete_occupation.visibility =View.GONE
               initIndustryRv()
           }
           R.id.txt_select_particular_vacation -> {
               if(ClickUtils.isDoubleClick(txt_select_particular_vacation)) {
                   return
               }
               if(!isVacationSelected) {
                   CommonToast.show(this@OritationVocationActivity,"请先选择职业类型")
                   return
               }
               if(!isIndustrySelected) {
                   CommonToast.show(this@OritationVocationActivity,"请先选择行业")
                   return
               }
               ll_industry.visibility = View.GONE
               ll_concrete_occupation.visibility = View.VISIBLE
               ll_three.visibility = View.GONE
               initConCreateOccupationRv()
           }
           R.id.btn_sure -> {
               if(ClickUtils.isDoubleClick(btn_sure)) {
                   return
               }
               if(!isVacationSelected) {
                   CommonToast.show(this@OritationVocationActivity,"请先选择职业类型")
                   return
               }
               if(!isIndustrySelected) {
                   CommonToast.show(this@OritationVocationActivity,"请先选择行业")
                   return
               }
               if(!isConcreateOccupationSelected) {
                   CommonToast.show(this@OritationVocationActivity,"请先选择具体职业")
                   return
               }
               finishWithResult(txt_select_particular_vacation.text.toString().trim())
           }
       }
    }

    private fun initIndustryRv() {
        listIndustry = ArrayList()
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        listIndustry!!.add("哈哈哈哈")
        industryAdapter = IndustryAdapter(listIndustry)

        val layoutManager = LinearLayoutManager(this@OritationVocationActivity)
        rv_industry.layoutManager = layoutManager
        rv_industry.adapter = industryAdapter
        industryAdapter!!.setOnItemClickListener(object: BaseAdapter.OnItemClick<String> {
            override fun OnItemClick(pos: Int, data: String?) {
                ll_industry.visibility = View.GONE
                ll_two.visibility = View.VISIBLE
                ll_three.visibility = View.VISIBLE
                isIndustrySelected = true
                txt_industry.text = data
            }
        })
    }

    private fun initConCreateOccupationRv() {
        listConcreateOccupation = ArrayList()
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        listConcreateOccupation!!.add("AAAAAA")
        concreateOccupationAdapter = ConcreteOccupationAdapter(listConcreateOccupation)
        rv__concrete_occupation
        val layoutManager = LinearLayoutManager(this@OritationVocationActivity)
        rv__concrete_occupation.layoutManager = layoutManager
        rv__concrete_occupation.adapter = concreateOccupationAdapter
        concreateOccupationAdapter!!.setOnItemClickListener(object: BaseAdapter.OnItemClick<String> {
            override fun OnItemClick(pos: Int, data: String?) {
                ll_concrete_occupation.visibility = View.GONE
                ll_three.visibility = View.VISIBLE
                isConcreateOccupationSelected = true
                txt_select_particular_vacation.text = data
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode) {
                REQUEST_CODE_ONE -> {
                    if(resultCode == 100) {
                        vacation_Selected = data!!.getStringExtra("data_function").toString()
                        LogUtils.e(TAG,vacation_Selected)
                        txt_orientate_type_cocation_and_function.text = vacation_Selected
                        isVacationSelected = true
                    }
                }
            REQUEST_CODE_TWO -> {
                if(resultCode == 100) {
                    vacation_Selected = data!!.getStringExtra("data_function").toString()
                    LogUtils.e(TAG,vacation_Selected)
                    txt_orientate_type_cocation_and_function.text = vacation_Selected
                }
            }
        }
    }

    private fun finishWithResult(value: String) {
        val intent = Intent()
        intent.putExtra("data_occupation", value)
        setResult(RESULT_CODE, intent)
        finish()
    }

}